<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            padding: 50px;
        }
        .box{
            width: 500px;
            margin-bottom: 30px;
        }
        .input-1 {
            width: 400px;
            height: 30px;
            border: 1px solid #f5f5f5;
            display: inline-block;
        }

        .input-2 {
            width: 80px;
            height: 30px;
            background-color: #3385ff;
            color: #fff;
            border: 0;
        }

        p {
            font-size: 16px;
            width: 80%;
            word-break: break-all;
        }
    </style>
</head>

<body>
<div class="box">
    公钥：<input type="text" id="publickey"  class="input-1" value="0499ebd5d2fa6d3cd2e3e2fca54593fd68f85b02d2c9384245b26081ba4622a17c96c39f98f179d8e7578c14d7e2442eaf03003c9dcc45eb0537417a59546c8218"><br>
    文本：<input type="text" id="entext"  class="input-1" value="Hello World! Hello World! Hello World! Hello World!"><br>
    <button id="en-submit1" type="button" class="input-2" onclick="onClickedbox1()">
        加密
    </button>
    <p id="encrypt1">加密结果：</p>
</div>
<div class="box">
    私钥：<input type="text" id="privatekey" class="input-1" value="795e7bba3dfdc963a06641bb554a3f1557b0bd2e93642bbd5aebbe4db7592fa0"><br>
    密文：<input type="text" id="encryptdata" class="input-1" value="0406a740c4a03adb22cb4d7a2e85be3bd536e2167eaf762dc67124ccacd381fe0e2af87c062113fe8e6c9a2fdd60e98e0fb779871b83132a309aa901d84b48fb77b8d79606034eacc2bb874c3a6744f43aea5a3d92c290daf066517407531bb9db4a999cc2b9db318192d8035be2571a47e0bc0cc8ebc9bdc87ba097ee8ea7df1933457c1f3e0b6f5dd8c6c5e4e5792c06ca0ffcd1"><br>
    <button id="en-submit2" type="button" class="input-2" onclick="onClickedbox2()">
        解密
    </button>
    <p id="encrypt2">解密结果：</p>
</div>
<div class="box">
    私钥：<input type="text" id="box3text" class="input-1" value="abc"><br>
    <button id="en-submit3" type="button" class="input-2" onclick="onClickedbox3()">
        解密
    </button>
    <p id="encrypt3">结果：</p>
</div>
<div class="box">
    <button id="click-me-button" type="button" onclick="addClickedMessage()">
        Click me!
    </button>
    <p id="chickme"></p>
</div>


<!-- Include Scala.js compiled code -->
<script type="text/javascript" src="./target/scala-2.13/sm2scala-sbt-fastopt.js"></script>
<script>
/**
        var enBtn1 = document.querySelector('#en-submit1');
        var enBtn2 = document.querySelector('#en-submit2');
        */
        //加密
        var publickey= '0499ebd5d2fa6d3cd2e3e2fca54593fd68f85b02d2c9384245b26081ba4622a17c96c39f98f179d8e7578c14d7e2442eaf03003c9dcc45eb0537417a59546c8218';
        var privitekey = '795e7bba3dfdc963a06641bb554a3f1557b0bd2e93642bbd5aebbe4db7592fa0';
        var encryptdata = '0406a740c4a03adb22cb4d7a2e85be3bd536e2167eaf762dc67124ccacd381fe0e2af87c062113fe8e6c9a2fdd60e98e0fb779871b83132a309aa901d84b48fb77b8d79606034eacc2bb874c3a6744f43aea5a3d92c290daf066517407531bb9db4a999cc2b9db318192d8035be2571a47e0bc0cc8ebc9bdc87ba097ee8ea7df1933457c1f3e0b6f5dd8c6c5e4e5792c06ca0ffcd1';
        var entext = 'Hello World! Hello World! Hello World! Hello World!';
        enBtn1.onclick = function () {
            var str = document.querySelector('#en-content1').value;
            var publickey =document.querySelector('#publickey').value;
            var privitekey='ED87F1FAAA6CBD668110D3026BD0CCB2A18C9313209F72822766AF569E9B1DD8';
            var plaintext = update(str); //补位
            var plaintext1=Bytes2Str(plaintext);
            var encryptData = sm2Encrypt(plaintext1, publickey);//加密
            document.getElementById('encrypt1').innerText=encryptData;
            var ds=sm2Decrypt(encryptData,privitekey);//解密
            console.log(ds);
        }

        //十六进制字符串转字节数组
        function Str2Bytes(str) {
            var pos = 0;
            var len = str.length;
            if (len % 2 != 0) {
                return null;
            }
            len /= 2;
            var hexA = new Array();
            for (var i = 0; i < len; i++) {
                var s = str.substr(pos, 2);
                var v = parseInt(s, 16);
                hexA.push(v);
                pos += 2;
            }
            return hexA;
        }
        //字节数组转字符串
        function Bytes2Str(arr)

        {
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                var tmp = arr[i].toString(16);
                if (tmp.length == 1) {
                    tmp = "0" + tmp;
                }
                str += tmp;
            }
            return str;
        }
    </script>
</body>
</html>